/**
 * 飞机列表
 * @author williamJM
 * date 2018/04/17 19:45
 */

"use strict";

define(['app',"config/pathConfig","layer"],function(app,pathConfig,layer){
    app.registerController('ctrl.aircraft.aircraftList',function($scope,$http,DTOptionsBuilder, DTColumnDefBuilder,$state){

        //获取所有飞机数据
        $http.get(pathConfig.backstagePath.getPath() + "/aircraft").then(function successCallback(response) {
                $scope.aircraftList = response.data.data;
            });

        $scope.dtColumnDefs = [
            DTColumnDefBuilder.newColumnDef(0).notSortable(),
            DTColumnDefBuilder.newColumnDef(8).notSortable()
        ];


        $scope.open = function (aircraft,ele) {
            var e = angular.element(ele.target);
            if(e.hasClass("row-details-close")) {
                e.removeClass("row-details-close").addClass("row-details-open");
                e.parent().parent().after(' <tr>\n' +
                    '                    <td class="details" colspan="9">\n' +
                    '                        <table>\n' +
                    '                            <tbody>\n' +
                    '                            <tr>\n' +
                    '                                <td>最大零油重:</td>\n' +
                    '                                <td>'+aircraft.maxZeroFuel+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>最大停机坪重量:</td>\n' +
                    '                                <td>'+aircraft.maxRamp+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>载油量:</td>\n' +
                    '                                <td>'+aircraft.fuelCapacity+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>滑行燃油:</td>\n' +
                    '                                <td>'+aircraft.taxiFuel+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>APU燃油:</td>\n' +
                    '                                <td>'+aircraft.apuBurn+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>进近燃油:</td>\n' +
                    '                                <td>'+aircraft.approachBurn+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>引擎除冰:</td>\n' +
                    '                                <td>'+aircraft.engineIce+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>总除冰燃油:</td>\n' +
                    '                                <td>'+aircraft.totalIce+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>起飞燃油:</td>\n' +
                    '                                <td>'+aircraft.takeoffBurn+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>机长:</td>\n' +
                    '                                <td>'+aircraft.length+' m</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>机宽:</td>\n' +
                    '                                <td>'+aircraft.width+' m</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>机高:</td>\n' +
                    '                                <td>'+aircraft.height+' m</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>最大速度:</td>\n' +
                    '                                <td>'+aircraft.maxSpeed+' 马赫</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>平均巡航速度:</td>\n' +
                    '                                <td>'+aircraft.avgSpeed+' 马赫</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>最大载客重量:</td>\n' +
                    '                                <td>'+aircraft.maxCargo+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>最大载货重量:</td>\n' +
                    '                                <td>'+aircraft.maxPax+' kg</td>\n' +
                    '                            </tr>\n' +
                    '                            <tr>\n' +
                    '                                <td>最大航程:</td>\n' +
                    '                                <td>'+aircraft.maxRange+' nm</td>\n' +
                    '                            </tr>\n' +
                    '                            </tbody>\n' +
                    '                        </table>\n' +
                    '                    </td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '                    <td hidden></td>\n' +
                    '\n' +
                    '                </tr>')
            } else {
                e.removeClass("row-details-open").addClass("row-details-close");
                e.parent().parent().next().remove()
            }
        };

        var deleteAircraft = function(aircraftId) {
            return new Promise(function (success,error) {
                $http.delete(pathConfig.backstagePath.getPath() + "/aircraft/" + aircraftId)
                    .then(function (response) {
                        success(response.data);
                    })
            })
        };


        $scope.edit = function(aircraftId) {

        };

        $scope.delete = function (aircraftId) {
            layer.confirm('确定要删除当前的飞机吗？', {
                icon: 3,
                title:'删除提示',
                btn: ['确定','取消']
            }, function () {
                deleteAircraft(aircraftId).then(function (response) {
                    if(response.status === 200) {
                        layer.msg('删除成功！', {icon: 1,time:1500});
                        $state.reload("index.aircraftList");
                        return;
                    }
                    layer.msg("服务器错误！", {icon: 2,time:1500});
                })
            });
        }






    });
});
